//工程化--模块化
import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter)

const router = new VueRouter({
    routes:[
        {
            path:'/',
            redirect: {name:'index'}
        },
        {
            name:'login',
            path:'/login',
            component: () => import('../views/login.vue')
        },
        {
            name:'index',
            path:'/index',
            component: () => import('../views/index.vue'),
            redirect: {name:'hello'},
            children:[
                {
                name:'hello',
                path:'hello',
                component: () => import('../views/hello.vue')
                },
                {
                name:'userManager',
                path:'userManager',
                component: () => import('../views/userManager.vue')
                },
                {
                name:'articalManager',
                path:'articalManager',
                component: () => import('../views/articalManager.vue')
                },
                {
                name:'cateManager',
                path:'cateManager',
                component: () => import('../views/cateManager.vue')
                },
                {
                name:'articalAdd',
                path:'articalAdd',
                component: () => import('../views/articalAdd.vue')
                },
            ]
        },
    ]
})

//导航守卫
router.beforeEach((to, from, next) => {
    if (to.path === '/login') {
        next()
    }else{
        let token = localStorage.getItem('heimaHoutai')
        if (token) {
            next()
        }else{
            next({path:'/login'})
        }
    }
    
})
export default router